package com.skeleton.server.blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.common.base.service.impl.BaseServiceImpl;
import com.skeleton.db.dao.IBlogArticleDao;
import com.skeleton.db.dao.IBlogTagDao;
import com.skeleton.db.dao.IBlogTagUserDao;
import com.skeleton.db.dto.BlogArticleDto;
import com.skeleton.db.dto.BlogTagDto;
import com.skeleton.db.dto.BlogTagUserDto;
import com.skeleton.db.entity.BlogArticle;
import com.skeleton.db.entity.BlogTag;
import com.skeleton.db.entity.BlogTagUser;
import com.skeleton.server.blog.service.IBlogService;
import com.skeleton.server.user.dto.BlogPublishDto;
import com.skeleton.utils.SystemConstants;

@Service
public class BlogServiceImpl extends BaseServiceImpl implements IBlogService {

	@Resource
	private IBlogArticleDao iBlogArticleDao;
	@Resource
	private IBlogTagDao iBlogTagDao;
	@Resource
	private IBlogTagUserDao iBlogTagUserDao;

	@Override
	public ResultDto doBlogPublish(BlogPublishDto bpDto) {
		logger.debug("-------BlogServiceImpl---doBlogPublish-----------------");
		ResultDto rd = new ResultDto();

		BlogArticleDto baDto = bpDto.getBad();
		List<BlogTagDto> btdList = bpDto.getBtdList();
		List<BlogTagUserDto> btudList = bpDto.getBtudList();

		BlogArticle baEntity = baDto.toEntity();
		iBlogArticleDao.insert(baEntity);

		for (int i = 0; i < btdList.size(); i++) {
			BlogTagDto btDto = btdList.get(i);
			BlogTag btEntity = btDto.toEntity();

			BlogTag bt = iBlogTagDao.selectByTagname(btEntity);
			if (bt == null) {
				iBlogTagDao.insert(btEntity);
			} else {
				bt.setUse_cnt(bt.getUse_cnt() + 1);
				iBlogTagDao.update(bt);
			}
		}

		for (int i = 0; i < btudList.size(); i++) {
			BlogTagUserDto btuDto = btudList.get(i);
			BlogTagUser btuEntity = btuDto.toEntity();

			BlogTagUser btu = iBlogTagUserDao.selectByTagname(btuEntity);
			if (btu == null) {
				iBlogTagUserDao.insert(btuEntity);
			}
		}

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getBlogArticleListByPage(int pageNum) {
		logger.debug("-------BlogServiceImpl---getBlogArticleListByPage-----------------");
		ResultDto rd = new ResultDto();

		List<BlogArticle> list = iBlogArticleDao.selectBlogArticleListByPage(pageNum * SystemConstants.PAGE_QUERY_BLOG_ARTICLE_CNT, SystemConstants.PAGE_QUERY_BLOG_ARTICLE_CNT);

		for (int i = 0; i < list.size(); i++) {
			BlogArticle ba = list.get(i);
			BlogArticleDto bad = ba.toDto();
			rd.addToDtoMap("BlogArticleDtoList", bad);
		}
		int total = iBlogArticleDao.countAll();
		rd.addToOtherMap("total", total);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getBlogArticleById(BlogArticleDto baDto) {
		logger.debug("-------BlogServiceImpl---getBlogArticleById-----------------");
		ResultDto rd = new ResultDto();

		BlogArticle baEntity = baDto.toEntity();

		BlogArticle ba = iBlogArticleDao.selectOneById(baEntity);

		BlogArticleDto bad = ba.toDto();

		rd.addToDtoMap("BlogArticleDto", bad);
		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doAddBlogArticleVisitCnt(BlogArticleDto baDto) {
		logger.debug("-------BlogServiceImpl---doAddBlogArticleVisitCnt-----------------");
		ResultDto rd = new ResultDto();
		
		BlogArticle baEntity = baDto.toEntity();
		iBlogArticleDao.addBlogArticleVisitCnt(baEntity);

		rd.setSuccess(true);
		return rd;
	}
	
	@Override
	public ResultDto doAddBlogArticleFavourCnt(BlogArticleDto baDto) {
		logger.debug("-------BlogServiceImpl---doAddBlogArticleFavourCnt-----------------");
		ResultDto rd = new ResultDto();

		BlogArticle baEntity = baDto.toEntity();
		iBlogArticleDao.addBlogArticleFavourCnt(baEntity);

		rd.setSuccess(true);
		return rd;
	}

}
